﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTask.Models;

namespace MVCTask.Controllers
{

    public class TaskController : Controller
    {
        private TaskContext db = new TaskContext();

        //
        // GET: /Task/
        [Authorize]
        public ViewResult Index()
        {
            // Get tasks of current user
            var tasks = db.Tasks.Include(t => t.Status).Include(t => t.Category).
                Where(t=> t.User == User.Identity.Name).OrderByDescending(t => t.ID);
            return View(tasks.ToList());

        }
        /*
        public ActionResult getJSON()
        {
            var jsonTasks = from m in db.Tasks.Include(t => t.Status).Include(t => t.Category)
                            select new
                            {
                                m.ID,
                                m.Description,
                                m.Details,
                                Category = new { m.Category.ID, m.Category.Description },
                                Status = new { m.Status.ID, m.Status.Description }
                            };
            return Json(jsonTasks, JsonRequestBehavior.AllowGet);
        }
        */
        //
        // GET: /Task/Details/5
        [Authorize]
        public ViewResult Details(int id)
        {
            //TODO: Security breach, any user can see any task once logged in
            Task task = (from m in db.Tasks.Include(t => t.Status).Include(t => t.Category)
                         where m.ID == id
                         select m).First();
            return View(task);
        }

        //
        // GET: /Task/Create
        [Authorize]
        public ActionResult Create()
        {
            ViewBag.StatusId = new SelectList(db.Status, "ID", "Description");
            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Description");
            return View();
        }

        //
        // POST: /Task/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                task.User = User.Identity.Name;
                db.Tasks.Add(task);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StatusId = new SelectList(db.Status, "ID", "Description", task.StatusId);
            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Description", task.CategoryId);
            return View(task);
        }

        //
        // GET: /Task/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            Task task = db.Tasks.Find(id);
            ViewBag.StatusId = new SelectList(db.Status, "ID", "Description", task.StatusId);
            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Description", task.CategoryId);
            return View(task);
        }

        //
        // POST: /Task/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {
                task.User = User.Identity.Name;
                db.Entry(task).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusId = new SelectList(db.Status, "ID", "Description", task.StatusId);
            ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Description", task.CategoryId);
            return View(task);
        }

        //
        // GET: /Task/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            Task task = db.Tasks.Find(id);
            return View(task);
        }

        //
        // POST: /Task/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Task task = db.Tasks.Find(id);
            db.Tasks.Remove(task);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}